$(document).ready(() => {
    const loginForm = document.getElementById('loginForm');
    
    loginForm.addEventListener('submit', (e) => {
        e.preventDefault();
        
        const formData = new FormData(e.target);
        const loginData = Object.fromEntries(formData.entries());
        
        console.log('Attempting login with:', loginData);
        
        fetch('http://localhost:8080/api/auth/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(loginData)
        })
        .then(async response => {
            const text = await response.text();
            console.log('Raw response:', text);
            
            if (!response.ok) {
                throw new Error(text || '登录失败');
            }
            
            return JSON.parse(text);
        })
        .then(data => {
            console.log('Login successful:', data);
            localStorage.setItem('token', data.token);
            localStorage.setItem('adminName', data.name);
            window.location.href = 'index.html';
        })
        .catch(error => {
            console.error('Login error:', error);
            alert(error.message);
        });
    });
}); 